import { defineStore } from "pinia";
import requests from "../utils/requests";
const useRegisterStore=defineStore('register',{
	state:()=>({
		form:{
			username:'',
			password:'',
			confirmPassword:'',
			email:'',
			phone:'',
			code:''
		}
	}),
	actions:{
		async registerUser(){
			const res=await requests({
				url:'/base/register/',
				method:'post',
				data:{
					username:this.form.username,
					password:this.form.password,
					email:this.form.email,
					phone:this.form.phone,
					emailCode:this.form.code
				}
			})
			if(res.code===200){
				this.form.username=''
				this.form.password=''
				this.form.confirmPassword=''
				this.form.email=''
				this.form.phone=''
				this.form.code=''
				uni.$u.toast('注册成功')
			}else{
				uni.$u.toast(`注册失败，${res.error}`)
			}
		},
		async send_email(){
			const res=await requests({
				url:'/base/sendEmail/',
				method:'post',
				data:{
					email:this.form.email
				}
			})
			if(res.code===200){
				 return '邮箱验证码发送成功'
			}else{
				return `邮箱验证码发送失败,${res.error}`
			}
		},
		async getCode(uCode){
			if (uCode.canGetCode) {
				// 模拟向后端请求验证码
				uni.showLoading({
					title: '正在获取验证码'
				})
				const msg=await this.send_email()
				console.log(msg)
				setTimeout(() => {
					uni.hideLoading();
					// 这里此提示会被this.start()方法中的提示覆盖
					uni.$u.toast(msg);
					// 通知验证码组件内部开始倒计时
					uCode.start();
				}, 2000);
			} else {
				uni.$u.toast('请30秒后再发送');
			}
		}
	}
})
export default useRegisterStore;
